package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.admin.api.entity.SysDept;
import com.engine4cloud.tdf.admin.api.feign.RemoteDeptService;
import com.engine4cloud.tdf.common.core.constant.SecurityConstants;
import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.CorAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.core.util.RetOps;
import com.engine4cloud.tdf.supervision.dto.SpAuditDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpMigrateResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxAuditDepartUpdateDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxUpdateStatusDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpNormalPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.service.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Slf4j
public class SpMigrateAuditServiceImpl implements SpMigrateAuditService {
    @Resource
    private SpAuditService auditService;
    @Resource
    private SpAuthorizeService authorizeService;
    @Resource
    private SpMigrateService migrateService;
    @Resource
    private JzdxService jzdxService;
    @Resource
    private JzdxAuditResultService auditResultService;
    @Resource
    private RemoteDeptService remoteDeptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(SpAuditDTO dto) {
        SpMigrateResultDTO old = Optional.ofNullable(migrateService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        migrateService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.VILLAGE_AUDIT));
        return auditService.villageAudit(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.MIGRATE, AllAuditActionStatusEnum.VILLAGE_AUDIT));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean refuse(SpAuditDTO dto) {
        SpMigrateResultDTO old = Optional.ofNullable(migrateService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        migrateService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.REFUSE));
        return auditService.refuse(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.MIGRATE, AllAuditActionStatusEnum.REFUSE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean authorize(SpAuditDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象不存在"));

        SpMigrateResultDTO old = Optional.ofNullable(migrateService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        //审核流程
        migrateService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.AUTHORIZE));
        authorizeService.authorize(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.MIGRATE, AllAuditActionStatusEnum.AUTHORIZE));

        //获取迁入地区矫正机构
        R<SysDept> deptRemote = remoteDeptService.detail(old.getQrdswtdsfs(), SecurityConstants.FROM_IN);
        SysDept dept = RetOps.of(deptRemote).getData()
                .orElseThrow(() -> new ValidateCodeException("迁入目的地区找不到对应的矫正机构"));

        //转移部门
        jzdxService.changeDepart(new JzdxAuditDepartUpdateDTO(dto.getRybh(), dept.getDeptId()), CorAuditActionStatusEnum.MIGRATE);

        //设置矫正对象的状态
        auditResultService.setStatus(new JzdxUpdateStatusDTO(dto.getRybh(), dx.getDeptId(), CorAuditActionStatusEnum.MIGRATE));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reject(SpAuditDTO dto) {
        SpMigrateResultDTO old = Optional.ofNullable(migrateService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        migrateService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.REJECT));
        return authorizeService.reject(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.MIGRATE, AllAuditActionStatusEnum.REJECT));
    }
}
